package org.builder.web.mvc;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.builder.bean.dyn.AntXml;
import org.builder.service.DynamicObjectsFactoryService;
import org.builder.service.MenuService;
import org.builder.web.validator.DefaultTaskValidator;
import org.builder.web.validator.TaskValidator;
import org.builder.xml.Menu;
import org.dynobjects.core.web.DynamicView;
import org.dynobjects.core.web.ServiceFactory;
import org.dynobjects.core.xml.View;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver;
import org.springframework.web.servlet.mvc.multiaction.MethodNameResolver;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;

public class TaskFormController extends SimpleFormController {

	private MethodNameResolver resolver = new InternalPathMethodNameResolver();

	private Map commandObjects = null;

	private Map successFormMap = null;

	private TaskValidator taskValidator = new DefaultTaskValidator();

	private DynamicObjectsFactoryService dynamicObjectsFactoryService;

	private DynamicView dynamicView = null;

	private ServiceFactory serviceFactory = null;
	
	private MenuService menuService= null;
	

	public Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String keyCommandClass = request.getParameter("task");
		
		if (keyCommandClass != null) {
			String commandObjectClassName = (String) commandObjects
					.get(keyCommandClass);
			if (commandObjectClassName != null) {

				Object commandObject = dynamicObjectsFactoryService
						.getInstance(commandObjectClassName);
				createView(request, keyCommandClass);

				return commandObject;
			}
		}
		return new Object();
	}

	private void createView(HttpServletRequest request, String keyCommandClass) {
		View view = dynamicView.getView(keyCommandClass);
		dynamicView.executeService(view, request);
		request.setAttribute("view", view);
	}

	private String getTaskName(HttpServletRequest request)
			throws NoSuchRequestHandlingMethodException {
		String keyCommandClass = resolver.getHandlerMethodName(request)
				.toLowerCase();
		return keyCommandClass;
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		Map err = new HashMap();
		if (taskValidator.check(command, err)) {
			AntXml antXml = (AntXml)command;
			Map model = new HashMap();
			model.put("menu", menuService.getMenuMap().get(request.getParameter("task")));
			ModelAndView modelAndView = new ModelAndView(getSuccessView(),model);
			return modelAndView;
		}
		Map model =errors.getModel();
		model.put("errors", err);
		createView(request, request.getParameter("task"));
		ModelAndView mw = showForm(request, response, errors,model);
//		System.out.println("ViewName: "
//				+ getSuccessFormMap().get(getTaskName(request)));
//		mw.setViewName((String) getSuccessFormMap().get(getTaskName(request)));
		return mw;
	}


	@Override
	protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
		Map model = new HashMap();
		model.put("menu", menuService.getMenuMap().get(request.getParameter("task")));
		return model;
	}


	/**
	 * 
	 * SPRING INJECTION
	 * 
	 */

	public void setCommandObjects(Map commandObjects) {
		this.commandObjects = commandObjects;
	}

	public Map getSuccessFormMap() {
		return successFormMap;
	}

	public void setSuccessFormMap(Map successsFormMap) {
		this.successFormMap = successsFormMap;
	}

	public void setTaskValidator(TaskValidator taskValidator) {
		this.taskValidator = taskValidator;
	}

	public void setDynamicObjectsFactoryService(
			DynamicObjectsFactoryService dynamicObjectsFactoryService) {
		this.dynamicObjectsFactoryService = dynamicObjectsFactoryService;
	}

	public DynamicObjectsFactoryService getDynamicObjectsFactoryService() {
		return dynamicObjectsFactoryService;
	}

	public void setDynamicView(DynamicView dynamicView) {
		this.dynamicView = dynamicView;
	}

	public void setResolver(MethodNameResolver resolver) {
		this.resolver = resolver;
	}

	public void setServiceFactory(ServiceFactory serviceFactory) {
		this.serviceFactory = serviceFactory;
	}


	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

}
